package com.mjfeed.controller.shop.member;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.Member;
import com.mjfeed.entity.Order;
import com.mjfeed.entity.OrderItem;
import com.mjfeed.entity.Product;
import com.mjfeed.entity.RefundMethod;
import com.mjfeed.entity.ReturnMethod;
import com.mjfeed.service.MemberService;
import com.mjfeed.service.OrderService;
import com.mjfeed.service.ProductService;
import com.mjfeed.service.RefundMethodService;
import com.mjfeed.service.ReturnMethodService;
import com.mjfeed.util.Format;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;

/**
 * @description 退换货申请 
 * @author zhaominglei
 * @date 2014年10月20日
 */
@Controller("shopMemberReturnController")
@RequestMapping("/member/return")
public class ReturnController extends BaseController {
	@Resource(name="memberService")
	private MemberService memberService;
	@Resource(name="orderService")
	private OrderService orderService;
	@Resource(name="productService")
	private ProductService productService;
	@Resource(name="returnMethodService")
	private ReturnMethodService returnMethodService;
	@Resource(name="refundMethodService")
	private RefundMethodService refundMethodService;
	
	@RequestMapping(value = "/orderList" ,method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		Member member = memberService.getCurrent();
		if (member == null || member.getId() == null || member.getId() < 1) {
			return ERROR_VIEW;
		}
		paramBean.setUrl(getBasePath(request) + "member/return/orderList.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<Order> pagination = orderService.getPagination(paramBean, member);
		request.setAttribute("pagination", pagination);
		return "shop/member/return/orderList";
	}
	
	@RequestMapping(value = "/request" ,method=RequestMethod.GET)
	public String requestGet(Long id, Long pid, HttpServletRequest request, HttpServletResponse response, Model model){
		if (id == null || pid == null) {
			return ERROR_VIEW;
		}
		Order order = orderService.getById(Order.class, id);
		if (order == null) {
			return ERROR_VIEW;
		}
		Product product = productService.getById(Product.class, pid);
		if (product == null) {
			return ERROR_VIEW;
		}
		OrderItem orderItem = orderService.getOrderItem(order, product);
		request.setAttribute("orderItem", orderItem);
		request.setAttribute("returnMethods", returnMethodService.getAll(ReturnMethod.class));
		request.setAttribute("refundMethods", refundMethodService.getAll(RefundMethod.class));
		return "shop/member/return/request";
	}
	
	@RequestMapping(value = "/request" ,method=RequestMethod.POST)
	public String requestPost(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		Order order = orderService.getById(Order.class, id);
		if (order == null) {
			return ERROR_VIEW;
		}
		request.setAttribute("order", order);
		return "shop/member/return/returnList";
	}
}
